package bookshop.controller.backend;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bookshop.service.CustCommentService;
import bookshop.service.helper.CustCommentQueryHelper;
import bookshop.service.impl.CustCommentServiceImpl;
import bookshop.utils.Page;

public class ManageCommentServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		
		String act = request.getParameter("act");
		if("qryloadall".equals(act)){
			CustCommentQueryHelper helper =new CustCommentQueryHelper();
			
			if(request.getParameter("quryOrderId")!=null && request.getParameter("quryOrderId").length()!=0)
				helper.setOrderId(request.getParameter("quryOrderId"));
			
			if(request.getParameter("quryCustId")!=null && request.getParameter("quryCustId").length()!=0)
				helper.setCustId(request.getParameter("quryCustId"));
			
			if(request.getParameter("qryCommentYear")!=null&&request.getParameter("qryCommentYear").length()!=0){
				helper.setQryCommentYear(request.getParameter("qryCommentYear"));
			}
			
			if(request.getParameter("qryCommentMonth")!=null&&request.getParameter("qryCommentMonth").length()!=0){
				helper.setQryCommentMonth(request.getParameter("qryCommentMonth"));
			}
			
			if(request.getParameter("qryCommentDay")!=null&&request.getParameter("qryCommentDay").length()!=0){
				helper.setQryCommentDay(request.getParameter("qryCommentDay"));
			}
			
			Page page =new Page();
			if(request.getParameter("pageNo")!=null)
				page.setPageNo(Integer.parseInt(request.getParameter("pageNo")));
			
			CustCommentService commentService =  new CustCommentServiceImpl();
			
			request.setAttribute("page", commentService.loadCommentsByCondition(helper, page));
			request.getRequestDispatcher("jsps/backend/comment/mgr_comment.jsp").forward(request, response);
		}else if("remove".equals(act)){
			int id =  Integer.parseInt(request.getParameter("commentId"));
			
			CustCommentService commentService =  new CustCommentServiceImpl();
			commentService.delCustCommentById(id);
			
			response.sendRedirect("commentMgr?act=qryloadall");
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doGet(request, response);
	}

}
